@file:Suppress("unused")

package com.gmlive.common.sample.views

import android.annotation.SuppressLint
import android.content.Context
import android.database.DataSetObserver
import android.graphics.*
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.SoundEffectConstants
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.util.Pools
import androidx.core.view.isVisible
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnAdapterChangeListener
import com.gmlive.common.sample.R
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeUtils
import com.google.android.material.shape.MaterialShapeUtils
import java.lang.ref.WeakReference
import java.util.*

/**
 * 选择项突出的TabLayout, 该TabLayout为moca量身定制，高度79dp，其中5dp为阴影高度，74dp为真实高度
 * @Author Sven
 * @Date 2021/7/26
 * @Email jiangzhimin@inke.cn
 */
@SuppressLint("UnsafeExperimentalUsageError")
class BulgeTabLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    companion object {
        const val SHAPE_WIDTH = 26.98F + 17.32F
        const val SMALL_ANGLE = 60F
        const val SMALL_RADIUS = 20F
        const val LARGE_ANGLE = 120F
        const val LARGE_RADIUS = 66F
    }

    private val paint = Paint().apply {
        style = Paint.Style.FILL_AND_STROKE
        color = Color.WHITE
        //抗锯齿
        isAntiAlias = true
    }
    private val path = Path()

    private val shapeWidth = SHAPE_WIDTH.ipx * 2

    private var viewPager: ViewPager? = null
    private var pagerAdapter: PagerAdapter? = null
    private var pagerAdapterObserver: DataSetObserver? = null
    private var pageChangeListener: TabLayoutOnPageChangeListener? = null
    private var adapterChangeListener: AdapterChangeListener? = null
    private var setupViewPagerImplicitly = false

    private val selectedListeners = ArrayList<OnTabSelectedListener<Tab>>()
    private var currentVpSelectedListener: OnTabSelectedListener<Tab>? = null

    private val tabs = ArrayList<Tab>()
    private var selectedTab: Tab? = null

    private val tabPool: Pools.Pool<Tab> = Pools.SynchronizedPool(16)

    // Pool we use as a simple RecyclerBin
    private val tabViewPool: Pools.Pool<TabView> = Pools.SimplePool(12)

    private val arcRectF = RectF()

    private var tabConfigurationStrategy: TabConfigurationStrategy? = null

    init {
        setWillNotDraw(false)
        setLayerType(View.LAYER_TYPE_SOFTWARE,null)
    }

    fun setupWithViewPager(
        viewPager: ViewPager?,
        configurationStrategy: TabConfigurationStrategy? = null,
        autoRefresh: Boolean = true,
        implicitSetup: Boolean = false
    ) {
        tabConfigurationStrategy = configurationStrategy
        // If we've already been setup with a ViewPager, remove us from it
        pageChangeListener?.let {
            this.viewPager?.removeOnPageChangeListener(it)
        }
        adapterChangeListener?.let {
            this.viewPager?.removeOnAdapterChangeListener(it)
        }

        if (currentVpSelectedListener != null) {
            // If we already have a tab selected listener for the ViewPager, remove it
            removeOnTabSelectedListener(currentVpSelectedListener!!)
            currentVpSelectedListener = null
        }

        if (viewPager != null) {
            this.viewPager = viewPager

            // Add our custom OnPageChangeListener to the ViewPager
            if (pageChangeListener == null) {
                pageChangeListener = TabLayoutOnPageChangeListener(this)
            }
            pageChangeListener?.reset()
            viewPager.addOnPageChangeListener(pageChangeListener!!)

            // Now we'll add a tab selected listener to set ViewPager's current item
            currentVpSelectedListener = ViewPagerOnTabSelectedListener(viewPager).apply {
                addOnTabSelectedListener(this)
            }
            val adapter = viewPager.adapter
            adapter?.let { setPagerAdapter(it, autoRefresh) }

            // Add a listener so that we're notified of any adapter changes
            if (adapterChangeListener == null) {
                adapterChangeListener = AdapterChangeListener()
            }
            adapterChangeListener?.setAutoRefresh(autoRefresh)
            viewPager.addOnAdapterChangeListener(adapterChangeListener!!)

            // Now update the scroll position to match the ViewPager's current item
            setSelectedTabView(viewPager.currentItem)
        } else {
            // We've been given a null ViewPager so we need to clear out the internal state,
            // listeners and observers
            this.viewPager = null
            setPagerAdapter(null, false)
        }

        setupViewPagerImplicitly = implicitSetup
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val tabCount = getTabCount()
        val tabWidth = (width / (tabCount.toFloat()) + 0.5).toInt()
        val selectedPosition = getSelectedTabPosition()
        Log.i("BulgeTabLayout", "selectedPosition = $selectedPosition")
        path.reset()
        if (selectedPosition in 0 until tabCount) {
            val tabXCenter = (selectedPosition * tabWidth + tabWidth / 2).toFloat()
            val shapeStart = tabXCenter - shapeWidth / 2

            path.moveTo(0F, 29.px)
            path.lineTo(shapeStart, 29.px)
            // 左衔接弧线 (1像素偏移)
            arcRectF.left = shapeStart - 20F.px + 0.5F
            arcRectF.top = (29 - 40).px + 0.5F
            arcRectF.right = arcRectF.left + 40.px - 1
            arcRectF.bottom = arcRectF.top + 40.px - 1
            path.arcTo(arcRectF, 90F, -60F, false)
            // 中心主弧线
            arcRectF.left = tabXCenter - 33.px
            arcRectF.top = 5.px
            arcRectF.right = tabXCenter + 33.px
            arcRectF.bottom = 71.px
            path.arcTo(arcRectF, 210F, 120F, false)
            // 右衔接弧线
            arcRectF.left = shapeStart + shapeWidth - 20F.px + 0.5F
            arcRectF.top = (29 - 40).px + 0.5F
            arcRectF.right = arcRectF.left + 40.px - 1
            arcRectF.bottom = arcRectF.top + 40.px - 1
            path.arcTo(arcRectF, 150F, -60F, false)
            // 直线到右边
            path.lineTo(width.toFloat(), 29.px)
            // 右边
            path.lineTo(width.toFloat(), height.toFloat())
            // 底边
            path.lineTo(0F, height.toFloat())
            // 左边
            path.close()

            paint.maskFilter = BlurMaskFilter(5.px, BlurMaskFilter.Blur.SOLID)
            paint.color = 0x1A000000
            canvas?.drawPath(path, paint)

            paint.maskFilter = null
            paint.color = Color.WHITE
            canvas?.drawPath(path, paint)
        }
    }

    fun addOnTabSelectedListener(listener: OnTabSelectedListener<Tab>) {
        if (!selectedListeners.contains(listener)) {
            selectedListeners.add(listener)
        }
    }

    fun removeOnTabSelectedListener(listener: OnTabSelectedListener<Tab>) {
        selectedListeners.remove(listener)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        MaterialShapeUtils.setParentAbsoluteElevation(this)
        if (viewPager == null) {
            // If we don't have a ViewPager already, check if our parent is a ViewPager to
            // setup with it automatically
            val vp = parent
            if (vp is ViewPager) {
                // If we have a ViewPager parent and we've been added as part of its decor, let's
                // assume that we should automatically setup to display any titles
                setupWithViewPager(
                    vp, tabConfigurationStrategy,
                    autoRefresh = true,
                    implicitSetup = true
                )
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (setupViewPagerImplicitly) {
            // If we've been setup with a ViewPager implicitly, let's clear out any listeners, etc
            setupWithViewPager(null)
            setupViewPagerImplicitly = false
        }
    }

    fun setPagerAdapter(adapter: PagerAdapter?, addObserver: Boolean) {
        if (pagerAdapter != null && pagerAdapterObserver != null) {
            // If we already have a PagerAdapter, unregister our observer
            pagerAdapter!!.unregisterDataSetObserver(pagerAdapterObserver!!)
        }
        pagerAdapter = adapter
        if (addObserver && adapter != null) {
            // Register our observer on the new adapter
            if (pagerAdapterObserver == null) {
                pagerAdapterObserver = PagerAdapterObserver()
            }
            adapter.registerDataSetObserver(pagerAdapterObserver!!)
        }

        // Finally make sure we reflect the new adapter
        populateFromPagerAdapter()
    }

    private fun populateFromPagerAdapter() {
        removeAllTabs()
        pagerAdapter?.let {
            val adapterCount = it.count
            for (i in 0 until adapterCount) {
                val tab = newTab()
                require(tabConfigurationStrategy != null) {
                    "TabConfigurationStrategy must set by setupWidthViewPager"
                }
                tabConfigurationStrategy!!.onConfigureTab(tab, i)
                addTab(tab, false)
            }

            // Make sure we reflect the currently set ViewPager item
            if (viewPager != null && adapterCount > 0) {
                val curItem = viewPager!!.currentItem
                if (curItem != getSelectedTabPosition() && curItem < getTabCount()) {
                    selectTab(getTabAt(curItem))
                }
            }
        }
    }

    fun selectTab(tab: Tab?, updateIndicator: Boolean = true) {
        val currentTab = selectedTab
        if (currentTab == tab) {
            if (currentTab != null) {
                // dispatch reselected
                dispatchTabReselected(currentTab)
            }
        } else {
            if (updateIndicator) {
                val newPosition = tab?.position ?: -1
                Log.i(
                    "BulgeTabLayout",
                    "selectTab, updateIndicator = $updateIndicator, newPosition = $newPosition"
                )
                if (newPosition != -1) {
                    // set selected tab view
                    setSelectedTabView(newPosition)
                }
            }

            selectedTab = tab
            if (currentTab != null) {
                // dispatchTabUnselected
                dispatchTabUnselected(currentTab)
            }
            if (tab != null) {
                // dispatchTabSelected
                dispatchTabSelected(tab)
            }
        }
    }

    private fun setSelectedTabView(position: Int) {
        Log.i("BulgeTabLayout", "setSelectedTabView, tab index = $position")
        val tabCount = childCount
        if (position < tabCount) {
            for (index in 0 until tabCount) {
                getChildAt(index).apply {
                    isSelected = position == index
                    isActivated = position == index
                }
            }
        }
    }

    private fun dispatchTabReselected(tab: Tab) {
        Log.i("BulgeTabLayout", "dispatchTabReselected, tab index = ${tab.position}")
        for (i in selectedListeners.indices.reversed()) {
            selectedListeners[i].onTabReselected(tab)
        }
    }

    private fun dispatchTabSelected(tab: Tab) {
        Log.i("BulgeTabLayout", "dispatchTabSelected, tab index = ${tab.position}")
        for (i in selectedListeners.indices.reversed()) {
            selectedListeners[i].onTabSelected(tab)
        }
    }

    private fun dispatchTabUnselected(tab: Tab) {
        Log.i("BulgeTabLayout", "dispatchTabUnselected, tab index = ${tab.position}")
        for (i in selectedListeners.indices.reversed()) {
            selectedListeners[i].onTabUnselected(tab)
        }
    }

    fun getSelectedTabPosition(): Int {
        return selectedTab?.position ?: -1
    }

    fun addTab(tab: Tab, setSelected: Boolean) {
        addTab(tab, tabs.size, setSelected)
    }

    fun addTab(tab: Tab, position: Int, setSelected: Boolean) {
        require(!(tab.parent !== this)) { "Tab belongs to a different BulgeTabLayout." }
        configureTab(tab, position)
        addTabView(tab)
        if (setSelected) {
            tab.select()
        }
    }

    private fun createTabView(tab: Tab): TabView {
        var tabView: TabView? = tabViewPool.acquire()
        if (tabView == null) {
            tabView = TabView(context)
        }
        tabView.setTab(tab)
        tabView.isFocusable = true
        return tabView
    }

    private fun configureTab(tab: Tab, position: Int) {
        tab.position = position
        tabs.add(position, tab)
        val count = tabs.size
        for (i in position + 1 until count) {
            tabs[i].position = i
        }
    }

    private fun addTabView(tab: Tab) {
        val tabView = tab.view
        tabView?.isSelected = false
        tabView?.isActivated = false
        addView(tabView, tab.position, createLayoutParamsForTabs())
    }

    private fun createLayoutParamsForTabs(): LayoutParams {
        val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        lp.width = 0
        lp.weight = 1F
        return lp
    }

    fun removeTab(tab: Tab) {
        require(!(tab.parent !== this)) { "Tab does not belong to this BulgeTabLayout." }
        removeTabAt(tab.position)
    }

    fun removeTabAt(position: Int) {
        val selectedTabPosition = selectedTab?.position ?: 0
        removeTabViewAt(position)
        val removedTab: Tab = tabs.removeAt(position)
        removedTab.reset()
        releaseFromTabPool(removedTab)
        val newTabCount = tabs.size
        for (i in position until newTabCount) {
            tabs[i].position = i
        }
        if (selectedTabPosition == position) {
            selectTab(if (tabs.isEmpty()) null else tabs[0.coerceAtLeast(position - 1)])
        }
    }

    private fun removeAllTabs() {
        // Remove all the views
        for (i in childCount - 1 downTo 0) {
            removeTabViewAt(i)
        }
        val i: MutableIterator<Tab> = tabs.iterator()
        while (i.hasNext()) {
            val tab = i.next()
            i.remove()
            tab.reset()
            releaseFromTabPool(tab)
        }
        selectedTab = null
    }

    fun newTab(): Tab {
        val tab = createTabFromPool()
        tab.parent = this
        tab.view = createTabView(tab)
        return tab
    }

    private fun createTabFromPool(): Tab {
        var tab = tabPool.acquire()
        if (tab == null) {
            tab = Tab()
        }
        return tab
    }

    private fun releaseFromTabPool(tab: Tab): Boolean {
        return tabPool.release(tab)
    }

    fun getTabCount(): Int {
        return tabs.size
    }

    fun getTabAt(index: Int): Tab? {
        return if (index < 0 || index >= getTabCount()) null else tabs[index]
    }

    private fun removeTabViewAt(position: Int) {
        val view = getChildAt(position) as TabView?
        removeViewAt(position)
        if (view != null) {
            view.reset()
            tabViewPool.release(view)
        }
        requestLayout()
    }

    interface OnTabSelectedListener<T : Tab> {
        fun onTabSelected(tab: T)
        fun onTabUnselected(tab: T)
        fun onTabReselected(tab: T)
    }

    interface TabConfigurationStrategy {
        fun onConfigureTab(tab: Tab, position: Int)
    }

    open class Tab {
        var tag: Any? = null
        var icon: Drawable? = null
            set(value) {
                field = value
                updateView()
            }

        var text: CharSequence? = null
            set(value) {
                field = value

                updateView()
            }

        var position: Int = -1

        var parent: BulgeTabLayout? = null
        var view: TabView? = null

        fun setIcon(@DrawableRes resId: Int): Tab {
            require(parent != null) {
                "Tab not attached to a BulgeTabLayout"
            }

            icon = AppCompatResources.getDrawable(parent!!.context, resId)

            return this
        }

        fun setText(@StringRes resId: Int): Tab {
            require(parent != null) {
                "Tab not attached to a BulgeTabLayout"
            }

            text = parent!!.resources.getText(resId)

            return this
        }

        open fun getOrCreateBadge(): BadgeDrawable {
            return view!!.getOrCreateBadge()
        }

        open fun removeBadge() {
            view!!.removeBadge()
        }

        open fun getBadge(): BadgeDrawable? {
            return view?.getBadge()
        }

        fun select() {
            require(parent != null) {
                "Tab not attached to a BulgeTabLayout"
            }

            parent?.selectTab(this)
        }

        fun isSelected(): Boolean {
            require(parent != null) {
                "Tab not attached to a BulgeTabLayout"
            }

            return parent?.getSelectedTabPosition() == this.position
        }

        open fun updateView() {
            view?.update()
        }

        open fun reset() {
            parent = null
            view = null
            tag = null
            icon = null
            text = null
            position = -1
        }

        override fun toString(): String {
            return "Tab(tag=$tag, icon=$icon, text=$text, position=$position, parent=$parent, view=$view)"
        }

    }

    class TabView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : LinearLayout(context, attrs, defStyleAttr) {
        private var tab: Tab? = null
        private var textView: TextView
        private var iconView: ImageView
        private var badgeAnchorView: View? = null
        private var badgeDrawable: BadgeDrawable? = null

        private val iconViewLayoutChangedListener =
            OnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
                if (view.visibility == VISIBLE) {
                    tryUpdateBadgeDrawableBounds(view)
                }
            }

        init {
            orientation = VERTICAL
            gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
            isClickable = true

            inflate(context, R.layout.bulge_tab_view, this)
            textView = findViewById(R.id.tabLabel)
            iconView = findViewById(R.id.tabIcon)
        }

        fun setTab(tab: Tab?) {
            if (tab !== this.tab) {
                this.tab = tab
                update()
            }
        }

        fun reset() {
            setTab(null)
            isSelected = false
        }

        fun update() {
            val tab = this.tab
            tryUpdateBadgeAnchor()
            addIconOnLayoutChangeListener(iconView)
            // Finally update our selected state
            isSelected = tab != null && tab.isSelected()
        }

        private fun updateTextAndIcon() {
            Log.i("BulgeTabLayout", "TabView updateTextAndIcon, position = ${tab?.position}")
            val icon = if (tab?.icon != null) {
                DrawableCompat.wrap(tab!!.icon!!)
            } else {
                null
            }

            if (icon != null) {
                iconView.isVisible = true
                iconView.setImageDrawable(icon)
            } else {
                iconView.isVisible = false
                iconView.setImageDrawable(null)
            }

            val text = tab?.text
            if (!text.isNullOrBlank()) {
                textView.isVisible = true
                textView.text = text
            } else {
                textView.isVisible = false
                textView.text = null
            }
        }

        internal fun getOrCreateBadge(): BadgeDrawable {
            // Creates a new instance if one is not already initialized for this TabView.
            if (badgeDrawable == null) {
                badgeDrawable = BadgeDrawable.create(context)
            }
            tryUpdateBadgeAnchor()
            checkNotNull(badgeDrawable) { "Unable to create badge" }
            return badgeDrawable!!
        }

        internal fun getBadge(): BadgeDrawable? {
            return badgeDrawable
        }

        internal fun removeBadge() {
            if (badgeAnchorView != null) {
                tryRemoveBadgeFromAnchor()
            }
            badgeDrawable = null
        }

        private fun addIconOnLayoutChangeListener(view: View?) {
            if (view == null) {
                return
            }
            view.addOnLayoutChangeListener(iconViewLayoutChangedListener)
        }

        private fun tryUpdateBadgeAnchor() {
            if (!hasBadgeDrawable()) {
                return
            }
            if (tab?.icon != null) {
                if (badgeAnchorView !== iconView) {
                    tryRemoveBadgeFromAnchor()
                    // Anchor badge to icon.
                    tryAttachBadgeToAnchor(iconView)
                } else {
                    tryUpdateBadgeDrawableBounds(iconView)
                }
            }
        }

        private fun tryRemoveBadgeFromAnchor() {
            if (!hasBadgeDrawable()) {
                return
            }
            clipViewToPaddingForBadge(true)
            if (badgeAnchorView != null) {
                BadgeUtils.detachBadgeDrawable(
                    badgeDrawable,
                    badgeAnchorView!!
                )
                badgeAnchorView = null
            }
        }

        private fun tryUpdateBadgeDrawableBounds(anchor: View) {
            // Check that this view is the badge's current anchor view.
            if (hasBadgeDrawable() && anchor === badgeAnchorView) {
                BadgeUtils.setBadgeDrawableBounds(
                    badgeDrawable!!,
                    anchor,
                    null
                )
            }
        }

        private fun tryAttachBadgeToAnchor(anchorView: View?) {
            if (!hasBadgeDrawable()) {
                return
            }
            if (anchorView != null) {
                clipViewToPaddingForBadge(false)
                BadgeUtils.attachBadgeDrawable(
                    badgeDrawable!!, anchorView, null
                )
                badgeAnchorView = anchorView
            }
        }

        private fun clipViewToPaddingForBadge(flag: Boolean) {
            // Avoid clipping a badge if it's displayed.
            // Clip children / view to padding when no badge is displayed.
            clipChildren = flag
            clipToPadding = flag
            val parent = parent as ViewGroup?
            if (parent != null) {
                parent.clipChildren = flag
                parent.clipToPadding = flag
            }
        }

        private fun hasBadgeDrawable(): Boolean = badgeDrawable != null

        override fun drawableStateChanged() {
            super.drawableStateChanged()
            var changed = false
            val state = drawableState
            if (iconView.drawable != null && iconView.drawable.isStateful) {
                changed = changed or iconView.drawable.setState(state)
            }

            if (changed) {
                iconView.requestLayout() // 两种状态图片大小不一致，重新Layout刷新一下
            }
        }

        override fun setSelected(selected: Boolean) {
            super.setSelected(selected)
            updateTextAndIcon()
            textView.isVisible = selected
            iconView.isSelected = selected
        }

        override fun performClick(): Boolean {
            val handled = super.performClick()

            return if (tab != null) {
                if (!handled) {
                    playSoundEffect(SoundEffectConstants.CLICK)
                }
                tab!!.select()
                true
            } else {
                handled
            }
        }
    }

    class ViewPagerOnTabSelectedListener(private val viewPager: ViewPager) :
        OnTabSelectedListener<Tab> {
        override fun onTabSelected(tab: Tab) {
            viewPager.currentItem = tab.position
        }

        override fun onTabUnselected(tab: Tab) {
        }

        override fun onTabReselected(tab: Tab) {
        }
    }

    inner class PagerAdapterObserver internal constructor() : DataSetObserver() {
        override fun onChanged() {
            populateFromPagerAdapter()
        }

        override fun onInvalidated() {
            populateFromPagerAdapter()
        }
    }

    inner class AdapterChangeListener : OnAdapterChangeListener {
        private var autoRefresh = false
        override fun onAdapterChanged(
            viewPager: ViewPager,
            oldAdapter: PagerAdapter?,
            newAdapter: PagerAdapter?
        ) {
            if (viewPager === viewPager) {
                setPagerAdapter(newAdapter, autoRefresh)
            }
        }

        fun setAutoRefresh(autoRefresh: Boolean) {
            this.autoRefresh = autoRefresh
        }
    }

    class TabLayoutOnPageChangeListener(tabLayout: BulgeTabLayout) :
        ViewPager.OnPageChangeListener {
        private val tabLayoutRef: WeakReference<BulgeTabLayout> = WeakReference(tabLayout)
        private var previousScrollState = 0
        private var scrollState = 0
        override fun onPageScrollStateChanged(state: Int) {
            previousScrollState = scrollState
            scrollState = state
        }

        override fun onPageScrolled(
            position: Int, positionOffset: Float, positionOffsetPixels: Int
        ) {
        }

        override fun onPageSelected(position: Int) {
            Log.i("BulgeTabLayout", "onPageSelected, position = $position")
            val tabLayout = tabLayoutRef.get()
            if (tabLayout != null && tabLayout.getSelectedTabPosition() != position && position < tabLayout.getTabCount()) {
                // Select the tab, only updating the indicator if we're not being dragged/settled
                // (since onPageScrolled will handle that).
                /*val updateIndicator = (scrollState == ViewPager.SCROLL_STATE_IDLE
                        || (scrollState == ViewPager.SCROLL_STATE_SETTLING
                        && previousScrollState == ViewPager.SCROLL_STATE_IDLE))*/
                tabLayout.selectTab(tabLayout.getTabAt(position), true)
            }
        }

        fun reset() {
            scrollState = ViewPager.SCROLL_STATE_IDLE
            previousScrollState = scrollState
        }

    }
}